package webx.actions;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.JWindow;

public class ScreenShotTest {
 
 // 图片保存服务器路径	
 public static String saveWebPath = null;
 public static String jpgName = null;
 public static boolean closed = false;
 
 public static void main(String[] args){
	System.out.println(System.getProperty("file.separator"));
	 cutScreen();
 }
 
 public static String cutScreen(){
	 try {
		 ScreenShotTest.jpgName = null;
		 closed = false;
		 // System.out.println(System.currentTimeMillis());
		 EventQueue.invokeLater(new Runnable() { 
		   public void run(){
		     try {
		       ScreenShotWindow ssw=new ScreenShotWindow();
		       ssw.setVisible(true);
		     } catch (AWTException e) {
		       e.printStackTrace();
		     }
		   }
		 });
		 
		 for(int i=0;i<1000;i++){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
				break;
			}
			if(closed){
				//System.out.println(i+","+jpgName);
				//System.out.println("Go...");
				break;
			}
		 }
		 // System.out.println("OK!"+jpgName);
		 
     }catch(Throwable e) {
        e.printStackTrace();
     } 
	return ScreenShotTest.jpgName;
 }
 
}


/*
 * 截图窗口
 */
@SuppressWarnings("serial")
class ScreenShotWindow extends JWindow
{ 
 private int orgx, orgy, endx, endy;
    private BufferedImage image=null;
    private BufferedImage tempImage=null;
    private BufferedImage saveImage=null;
    private String sep = System.getProperty("file.separator");
    private ToolsWindow tools=null;

 public ScreenShotWindow() throws AWTException{
   //获取屏幕尺寸
   Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
   this.setBounds(0, 0, d.width, d.height);

   //截取屏幕
   Robot robot = new Robot();
   image = robot.createScreenCapture(new Rectangle(0, 0, d.width,d.height));

   this.addMouseListener(new MouseAdapter() {
    @Override
   public void mousePressed(MouseEvent e) {
    //鼠标松开时记录结束点坐标，并隐藏操作窗口
             orgx = e.getX();
             orgy = e.getY();

             if(tools!=null){
              tools.setVisible(false);
             }
   }
   @Override
   public void mouseReleased(MouseEvent e) {
    //鼠标松开时，显示操作窗口
    if(tools==null){
     tools=new ToolsWindow(ScreenShotWindow.this,e.getX(),e.getY());
    }else{
     tools.setLocation(e.getX(),e.getY());
    }
    tools.setVisible(true);
    tools.toFront();
   }
  });

   this.addMouseMotionListener(new MouseMotionAdapter() {

   @Override
   public void mouseDragged(MouseEvent e) {
    //鼠标拖动时，记录坐标并重绘窗口
                endx = e.getX();
                endy = e.getY();

                //临时图像，用于缓冲屏幕区域放置屏幕闪烁
                Image tempImage2=createImage(ScreenShotWindow.this.getWidth(),ScreenShotWindow.this.getHeight());
                Graphics g =tempImage2.getGraphics();
                g.drawImage(tempImage, 0, 0, null);
                int x = Math.min(orgx, endx);
                int y = Math.min(orgy, endy);
                int width = Math.abs(endx - orgx)+1;
                int height = Math.abs(endy - orgy)+1;
                // 加上1防止width或height0
                g.setColor(Color.BLUE);
                g.drawRect(x-1, y-1, width+1, height+1);
                //减1加1都了防止图片矩形框覆盖掉
                saveImage = image.getSubimage(x, y, width, height);
                g.drawImage(saveImage, x, y, null);
                ScreenShotWindow.this.getGraphics().drawImage(tempImage2,0,0,ScreenShotWindow.this);
   }
  });
 }

    @Override
    public void paint(Graphics g) {
        RescaleOp ro = new RescaleOp(0.8f, 0, null);
        tempImage = ro.filter(image, null);
        g.drawImage(tempImage, 0, 0, this);
    }
    //保存图像到文件
 public void saveImage() throws IOException {
  //文件过滤器，用户过滤可选择文件
  //FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG", "jpg");
  //jfc.setFileFilter(filter);

  //初始化一个默认文件（此文件会生成到桌面上）
     
     String jpgPath = ScreenShotTest.saveWebPath + "clientdata"+sep+UUID.randomUUID() + ".jpg";
     //写入文件
     File file = new File(jpgPath);
     // System.out.println(file.getAbsolutePath());
     ImageIO.write(saveImage,"jpg",file);
     ScreenShotTest.jpgName = file.getName();
  //}
 }
}
/*
 * 操作窗口
 */
@SuppressWarnings("serial")
class ToolsWindow extends JWindow
{
 private ScreenShotWindow parent;
 @SuppressWarnings("unused")
private String sep = System.getProperty("file.separator");
 public ToolsWindow(ScreenShotWindow parent,int x,int y) {
  this.parent=parent;
  this.init();
  this.setLocation(x, y);
  this.pack();
  this.setVisible(true);
 }

 private void init(){

  this.setLayout(new BorderLayout());
  JToolBar toolBar=new JToolBar("Java Cut");
  final ToolsWindow twin = this;
  //Activator.plugin_path = Activator.plugin_path.replaceFirst("/", "");
  // System.out.println("Activator.plugin_path:"+Activator.plugin_path);
  //保存按钮
  JButton saveButton=new JButton("YE");
  saveButton.addActionListener(new ActionListener() { 
   public void actionPerformed(ActionEvent e) {
    try {
    	parent.saveImage();
    } catch (Throwable e1) {
    	e1.printStackTrace();
    }finally{
    	ScreenShotTest.closed = true;
    	twin.dispose();
    	parent.dispose();
    }
   }
  });
  toolBar.add(saveButton);
  // System.out.println(new File("").getAbsolutePath());
  //关闭按钮
  JButton closeButton=null;
  closeButton = new JButton("NO");
  closeButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
	   ScreenShotTest.closed = true;
	   twin.dispose();
	   parent.dispose();
   }
  });
  toolBar.add(closeButton);

  this.add(toolBar,BorderLayout.NORTH);
 }
}
